package controllers;

import models.Vehicule;
import play.*;
import play.mvc.*;
import play.data.*;
import views.html.*;

public class Application extends Controller {
  
  public static Result index() {
    return redirect(routes.Application.vehicules());
  }
  
  static Form<Vehicule> vehiculeForm = form(Vehicule.class);
  
  //affiche liste des vehicules avec le formulaire de creation
  public static Result vehicules() {
    return ok(
    views.html.index.render(Vehicule.all(), vehiculeForm)
  );
  }
  
  //Creation d'un vehicule avec gestion de problemes
  public static Result newVehicule() {
    Form<Vehicule> filledForm = vehiculeForm.bindFromRequest();
		if(filledForm.hasErrors()) {
		return badRequest(
		views.html.index.render(Vehicule.all(), filledForm)
    );
  } else {
    Vehicule.create(filledForm.get());
    return redirect(routes.Application.vehicules());  
  }
  }
  
  public static Result deleteVehicule(Long id) {
    Vehicule.delete(id);
  return redirect(routes.Application.vehicules());
  }
  
}